package com.ruoyi.monitor.utils.Msg;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.monitor.Vo.PredictDataVo;
import com.ruoyi.monitor.domain.Pollutant;
import com.ruoyi.monitor.utils.Common.Constant;
import com.ruoyi.monitor.utils.Common.DateUtils;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class MsgUtil {
    static String[] name = {"CO","NO2","SO2","PM2.5","PM10","O3"};

    public String preForHours(PredictDataVo dataVo,Integer index){
        List<Integer> data = dataVo.getData();
        Integer integer = data.get(index);
        String date = dataVo.getDates()[index];
        return "预计"+date+"时，"+"空气污染指数将达到"+integer;
    }

    public String getSuggest(Pollutant pollutant){
        if (Constant.EXCELLENT_RANGE.equals(pollutant.getRange())){
            return Message.advice1;
        }else if (Constant.GOOD_RANGE.equals(pollutant.getRange())){
            return Message.advice2;
        }else if (Constant.MILD_RANGE.equals(pollutant.getRange())){
            return Message.advice3;
        }else if (Constant.MODERATE_RANGE.equals(pollutant.getRange())){
            return Message.advice4;
        }else if (Constant.SEVERE_RANGE.equals(pollutant.getRange())){
            return Message.advice5;
        }else {
            return Message.advice6;
        }
    }

    /**
     *
     * @param user
     * @param pollutant
     * @param extra 可以加上建议。
     * @return
     */
    public String warnMessage(SysUser user, Pollutant pollutant,String extra){

        String msg = Message.WARN_TEMPLATE;
        String res = msg.replaceAll("\\{name}", user.getUserName())
        .replaceAll("\\{aqi}", pollutant.getAqi().toString())
        .replaceAll("\\{quality}", pollutant.getQuality());
        res = res + extra;
        if (pollutant.getAqi()<50 && 50-pollutant.getAqi() < 10){
            res += "空气质量有由优转良趋势。";
        }
        else if (pollutant.getAqi()>50 && pollutant.getAqi()<100){
            if ( 100 - pollutant.getAqi() < 10){
                res += "空气质量有由良转轻度污染趋势。";
            }
            res += "请注意防范！\n";
        }else if (pollutant.getAqi()>100 && pollutant.getAqi()<150){
            if ( 150 - pollutant.getAqi() < 10){
                res += "空气质量有由轻度污染转中度污染趋势。";
            }
            res += "外出请佩戴好口罩，减少户外运动！\n";
        }else if (pollutant.getAqi()>150 && pollutant.getAqi()<200){
            if ( 150 - pollutant.getAqi() < 10){
                res += "空气质量有由中度污染转重度污染趋势。";
            }
            res += "外出请佩戴好口罩，避免户外运动！\n";
        }else if (pollutant.getAqi()>200 && pollutant.getAqi()<300){
            if ( 300 - pollutant.getAqi() < 10){
                res += "空气质量有由中度污染转严重污染趋势。";
            }
            res += "空气污染严重，外出请佩戴好口罩，减少出行！\n";
        }
        if (!"".equals(pollutant.getPrimary_pollutant())){
            res += "当前首要污染物为"+pollutant.getPrimary_pollutant()+"\n";
        }

        return  this.addPrimaryPollutant(pollutant,res);
    }

    /**
     *
     * @param user
     * @param now
     * @param past
     * @param extra 可以加上建议。
     * @return
     */
    public String warnMessage(SysUser user, Pollutant now,Pollutant past,String extra){
        String msg =  Message.WARN_2_TEMPLATE;
        String res = msg.replaceAll("\\{name}", user.getUserName())
                .replaceAll("\\{now.aqi}",now.getAqi().toString())
                .replaceAll("\\{now.quality}",now.getQuality())
                .replaceAll("\\{past.aqi}",past.getAqi().toString())
                .replaceAll("\\{past.quality}",past.getQuality());
        if (now.getAqi()<past.getAqi() && (past.getAqi()-now.getAqi()>=10)){
            msg = res+Message.UNDULATION_UP;
        }else if (now.getAqi()>past.getAqi() && (now.getAqi()-past.getAqi()>=10)){
            msg = res+Message.UNDULATION_DOWN;
        }else {
            msg = res+Message.UNDULATION_STABLE;
        }

        msg = msg + extra ;
        if (now.getAqi()>50){
            msg += "请注意防范！";
        }
        System.out.println(msg);
        return this.addPrimaryPollutant(now,res);
    }


    public String addPrimaryPollutant(Pollutant pollutant,String res){
        ArrayList<Float> n = new ArrayList<>();
        n.add(pollutant.getCo());
        n.add(pollutant.getNo2());
        n.add(pollutant.getSo2());
        n.add(pollutant.getPm25());
        n.add(pollutant.getPm10());
        n.add(pollutant.getO3());
        float max = n.get(0);
        String attainment = "";
        for (int i = 0; i < n.size(); i++) {
            if (n.get(i)>max){
                max = n.get(i);
                if (max >= 100){
                    attainment = name[i];
                }
            }
        }
        if (!"".equals(attainment)){
            if ("PM10".equals(attainment) || "PM2.5".equals(attainment)) attainment =
                    "颗粒物("+attainment+")";
            res += "当前超标污染物为"+attainment+"\n";
        }
        return res;
    }
}
